using System.Text;
using System.Threading;

namespace e3_ans
{
    public partial class Form1 : Form
    {
        string[] infileName = new string[2];
        FileStream[] fileIn = new FileStream[2];
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using(fileIn[0] = new FileStream((infileName[0] = "C:\\Users\\Jojo\\Documents\\CSharpWork\\TheFifthHomework\\e3_ans\\bin\\Debug\\net6.0-windows\\Data\\File1.txt"), FileMode.Open))
            {
                label3.Text = "File1 added successfully!";

                int b;
                byte[] bs = new byte[1024];
                while ((b = fileIn[0].Read(bs, 0, bs.Length)) != 0)
                {
                    textBox1.Text = Encoding.UTF8.GetString(bs, 0, b);
                }
                fileIn[0].Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (fileIn[1] = new FileStream((infileName[1] = "C:\\Users\\Jojo\\Documents\\CSharpWork\\TheFifthHomework\\e3_ans\\bin\\Debug\\net6.0-windows\\Data\\File2.txt"), FileMode.Open))
            {

                label3.Text = "File2 added successfully!";

                int b;
                byte[] bs = new byte[1024];
                while ((b = fileIn[1].Read(bs, 0, bs.Length)) != 0)
                {
                    textBox1.Text = Encoding.UTF8.GetString(bs, 0, b);
                }
                fileIn[1].Close();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {

            using (FileStream fileOut = new FileStream("C:\\Users\\Jojo\\Documents\\CSharpWork\\TheFifthHomework\\e3_ans\\bin\\Debug\\net6.0-windows\\Data\\merge.txt", FileMode.Create))
            {
                int b;
                for (int i = 0; i < fileIn.Length; i++)
                {
                    try
                    {
                        fileIn[i] = new FileStream(infileName[i], FileMode.Open);
                        while ((b = fileIn[i].ReadByte()) != -1)
                        {
                            fileOut.WriteByte((byte)b);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        fileIn[i].Close();
                    }
                }
                fileOut.Close();
                label3.Text = "Merge successfully!";
            }
            using (FileStream fileMerge = new FileStream("C:\\Users\\Jojo\\Documents\\CSharpWork\\TheFifthHomework\\e3_ans\\bin\\Debug\\net6.0-windows\\Data\\merge.txt", FileMode.Open))
            {
                int b;
                byte[] bs = new byte[1024];
                while ((b = fileMerge.Read(bs, 0, bs.Length)) != 0)
                {
                    textBox1.Text = Encoding.UTF8.GetString(bs, 0, b);
                }
                fileMerge.Close();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}